"""
Case Type   : 资源池化
Case Name   : 资源池化场景下, 暂停恢复cm之后，查看switchover是否正常
Create At   : 2024-04-07
Owner       : songshixuan
Description :
    1、查看集群状态 cm_ctl query -Cv
    2、执行cm_ctl pause暂停cm集群
    3、执行cm_ctl resume恢复cm集群
    4、stop住整个集群
    5、重新启动集群
    6、进行switchover并查看状态
    7、恢复集群原始状态
Expect      :
    1、查看集群状态 cm_ctl query -Cv
    2、集群暂停成功
    3、集群恢复成功
    4、stop成功
    5、集群启动成功
    6、switchover符合预期
    7、恢复集群原始状态
History     :
    1、2024-04-07 创建
"""

import os
import time
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:cm_ctl pause; expect:cm_ctl 暂停-----'
        self.log.info(text)
        status = Primary_SH.exec_gs_ssh('rm -rf $GAUSSHOME/bin/cluster_manual_starting')
        self.log.info(status)
        status = Primary_SH.exec_cm_ctl(mode='pause')
        self.log.info(status)
        self.assertIn('has been paused', status, '执行失败' + text)

        text = '-----step3:cm_ctl resume; expect:执行成功-----'
        self.log.info(text)

        status = Primary_SH.exec_cm_ctl(mode='resume')
        self.log.info(status)
        self.assertIn('has been resumed', status, '执行失败' + text)

        text = '-----step4:停止集群; expect:停止成功，主节点进入Manually stopped状态-----'
        self.log.info(text)
        # 运行failover脚本进行failover构建
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn(self.constant.cm_stop_success_msg, status,
                      '执行失败' + text)
        text = '-----step5:集群重新启动; expect:执行成功-----'
        self.log.info(text)
        startdn = Primary_SH.exec_cm_ctl('start')
        self.log.info(startdn)
        self.assertIn(self.constant.cm_start_success_msg, startdn,
                '拉起dn节点失败')
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        
        text = '-----step6:备1节点执行switchover切换为主节点; expect:执行成功-----'
        self.log.info(text)
        switch = Primary_SH.exec_cm_ctl(mode='switchover',
                                           param='-n 2',
                                           path=f'-D {macro.DB_INSTANCE_PATH}')
        self.log.info(switch)
        self.assertIn(self.constant.cm_switchover_success_msg, switch,
                      '执行失败' + text)

        text = '-----step7:再次查看切换后集群状态 expect:集群状态正常,备1切换为主节点，原主节点降为备节点-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        res1 = status.splitlines()[-1].split('|')[1].split('6002')[-1]
        res2 = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        self.log.info(res1)
        self.log.info(res2)
        self.assertIn('S Primary Normal', res1, '执行失败' + text)
        self.assertIn('P Standby Normal', res2, '执行失败' + text)

    def tearDown(self):
        text = '-----step8:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)
        status = Primary_SH.exec_cm_ctl(mode='resume')
        self.log.info(status)
        self.assertIn('has been resumed', status, '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='stop', param='')
        self.log.info(status)
        self.assertIn('successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start', param='')
        self.log.info(status)
        self.assertIn('successfully.', status, '执行失败' + text)
        
        recovery = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(recovery)
        self.assertIn(self.constant.cm_switchover_success_msg, recovery,
                      '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('P Primary Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                    status.splitlines()[-1].split('|')[2].split('6003')[-1],
                    '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')

